package com.platform.server.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.paltform.server.entity.SysUser;
import com.platform.server.dao.WithdrawDao;
import com.platform.server.service.MessageService;
import com.platform.server.util.StringTools;

@Controller
public class MessageController {

	@Autowired
	private MessageService messageService;
	
	@Autowired
	private WithdrawDao withdrawDao;
	
	/**
	 * 查看消息列表
	 */
	@RequestMapping("/selectMessageList")
	public Object selectMessageList(HttpServletRequest request,String updateid,String id,String startTime,String endTime,String status,String title){
		Map<String,Object> map = new HashMap<String,Object>();
		if(!StringTools.isEmpty(startTime) && !StringTools.isEmpty(endTime)){
			map.put("startTime", startTime);
			map.put("endTime", endTime);
		}
		if(!StringTools.isEmpty(status)){
			map.put("status", status);
		}
		if(!StringTools.isEmpty(title)){
			map.put("title", title);
		}
		if(!StringTools.isEmpty(id)){
			map.put("id", id);
		}
		if(!StringTools.isEmpty(updateid)){
			map.put("id", updateid);
		}
		List<Map<String, Object>> lists = messageService.selectMessageList(map);
		request.setAttribute("lists",lists);
		request.setAttribute("nums",lists.size());
		if(!StringTools.isEmpty(id)){
			return "/ui/message/message_info.jsp";
		}
		if(!StringTools.isEmpty(updateid)){
			return "/ui/message/update_message.jsp";
		}
		return "/ui/message/message_list.jsp";
	}
	
	/**
	 * 删除消息
	 */
	@RequestMapping("/deleteMessage")
	public Object deleteMessage(String idd){
		int flag  = messageService.deleteMessage(idd);
		return "/selectMessageList";
	}
	
	/**
	 * 添加消息
	 */
	@RequestMapping(value="/insertMessage",method = RequestMethod.POST)
	public void insertMessage(String title,String scope,String username,String content,String push_time,
			HttpServletResponse response,HttpSession session) throws Exception{
		SysUser su = (SysUser) session.getAttribute("user");
		if(su!=null){
			username = su.getSys_name();
		}
		if(scope.split(",").length == 7){
			scope = "全部";
		}
		int flag = messageService.insertMessage(title, scope, username, content, push_time);
		withdrawDao.insertLog(username, "发布了一条系统消息，标题为：" + title);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
	
	/**
	 * 编辑消息
	 */
	@RequestMapping(value="/updateMessage",method = RequestMethod.POST)
	public void updateMessage(String id,String title,String scope,String username,String content,String push_time,
			HttpServletResponse response,HttpSession session) throws Exception{
		SysUser su = (SysUser) session.getAttribute("user");
		if(su!=null){
			username = su.getSys_name();
		}
		if(scope.split(",").length == 7 || scope.split(",").length == 6){
			scope = "全部";
		}
		int flag = messageService.updateMessage(id,title, scope, username, content, push_time);
		withdrawDao.insertLog(username, "编辑了一条系统消息，标题为：" + title);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("flag", flag);
        response.getOutputStream().write(jsonObject.toString().getBytes("utf-8"));
	}
}
